package com.example.homework0113;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class Chat extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        initview();
    }
    private void initview(){
        RecyclerView chatlist=findViewById(R.id.list_chat);
        chatlist.setLayoutManager(new LinearLayoutManager(this));
        EditText editText=findViewById(R.id.edit_text);
        ImageView imageView=findViewById(R.id.btn_send);
        List<ChatModel>msgList=new ArrayList<>();
        ChatAdapter chatAdapter=new ChatAdapter(this);
        chatAdapter.setMsgData(msgList);
        chatlist.setAdapter(chatAdapter);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msgText=editText.getText().toString();
                if (!TextUtils.isEmpty(msgText)){
                    ChatModel sendmsg=new ChatModel();
                    sendmsg.setSendorRecve(true);
                    sendmsg.setUserIcon(R.drawable.houge);
                    sendmsg.setTextMsg(msgText);
                    msgList.add(sendmsg);
                    ChatModel recmsg=new ChatModel();
                    recmsg.setSendorRecve(false);
                    recmsg.setUserIcon(R.drawable.change);
                    recmsg.setTextMsg("你好，正在忙，一会回您");
                    msgList.add(recmsg);
                    chatAdapter.notifyDataSetChanged();
                    editText.setText("");
                    chatlist.smoothScrollToPosition(msgList.size()-1);
                }
            }
        });

    }
}